package com.xu.sms.controller.admin;

import com.xu.sms.entity.Info;
import com.xu.sms.entity.Type;
import com.xu.sms.service.InfoService;
import com.xu.sms.service.TypeService;
import com.xu.sms.utils.PageResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xupenghao@163.com
 * @date 2018-06-05 17:16
 */
@Controller
@RequestMapping("/admin/info")
public class AdminInfoController {

    @Resource
    private InfoService infoService;
    @Resource
    private TypeService typeService;

    @RequestMapping("/infoAddUI")
    public String infoAddUI(Model model){
        List<Type> typeList = typeService.findAll();
        model.addAttribute("typeList",typeList);
        return "admin/info/info_add";
    }

    @RequestMapping("/add")
    public String add(Info info,Model model){
        infoService.add(info);
        return infoListUI(null,null,null,model);
    }

    @RequestMapping("/descUI")
    public String descUI(Integer infoId,Model mode){
            mode.addAttribute("infoMap",infoService.findInfoAndTypeById(infoId));

        return "admin/info/info_desc";
    }

    @RequestMapping("/editUI")
    public String editUI(Integer infoId,Model model){
        List<Type> typeList = typeService.findAll();
        model.addAttribute("typeList",typeList);
        model.addAttribute("info",infoService.findInfoById(infoId));
        return "admin/info/info_edit";
    }

    @RequestMapping("/edit")
    public String edit(Info info,Model model){
        infoService.update(info);

        return infoListUI(null,null,null,model);
    }

    @RequestMapping("/delete")
    public String delete(Integer infoId,Model model){
        infoService.delete(infoId);

        return infoListUI(null,null,null,model);
    }





    @RequestMapping("/findInfoByTypeId")
    @ResponseBody
    public List<String> findInfoByTypeId(int typeId){
        List<String> infoList = infoService.findInfoByTypeId(typeId);
        return infoList;
    }

    @ResponseBody
    @RequestMapping("/findInfoBykeyWord")
    public List<String> findInfoBykeyWord(String keyword){

        return infoService.findInfoBykeyWord(keyword);
    }



    @RequestMapping("/infoListUI")
    public String infoListUI(Integer pCur,String keyword,
                             Integer typeId,
                             Model model){

        //查询出所有的类型
        List<Type> typeList = typeService.findAll();
        model.addAttribute("typeList",typeList);
        List<Info> infoList = infoService.findAllByPaging(pCur,keyword,typeId);
        model.addAttribute("infoList",infoList);
        model.addAttribute("counts",infoService.getTotalCount(keyword,typeId));
        model.addAttribute("pCur",(pCur ==null)?1:pCur);
        model.addAttribute("pSize", PageResult.PAGERESULT_PSIZE);
        model.addAttribute("keyword",keyword);
        model.addAttribute("typeId",typeId);
        model.addAttribute("keyword",keyword);
        model.addAttribute("typeId",typeId);

        return "admin/info/info_list";
    }

}
